<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <el-icon class="logo-icon"><Management /></el-icon>
        <h2>AdminPro 管理系统</h2>
      </div>
      
      <el-form 
        ref="loginFormRef"
        :model="loginForm" 
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="用户名" 
            prefix-icon="User"
            autocomplete="off"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="密码" 
            prefix-icon="Lock"
            show-password
          />
        </el-form-item>
        
        <el-form-item prop="code">
          <div class="code-input">
            <el-input 
              v-model="loginForm.code" 
              placeholder="验证码" 
              prefix-icon="Key"
              autocomplete="off"
            />
            <div class="code-img">
              <img src="https://picsum.photos/100/40" alt="验证码" @click="refreshCode" />
            </div>
          </div>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            class="login-btn" 
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElForm } from 'element-plus'
import { Management } from '@element-plus/icons-vue'

// 路由
const router = useRouter()

// 表单引用
const loginFormRef = ref(ElForm)

// 登录表单数据
const loginForm = ref({
  username: 'admin',
  password: '123456',
  code: ''
})

// 表单验证规则
const loginRules = ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
})

// 刷新验证码
const refreshCode = () => {
  // 实际项目中应该调用后端接口刷新验证码
  ElMessage.info('验证码已刷新')
}

// 处理登录
const handleLogin = async () => {
  try {
    // 表单验证
    await loginFormRef.value.validate()
    
    // 模拟登录请求
    setTimeout(() => {
      ElMessage.success('登录成功')
      // 跳转到首页
      router.push('/dashboard')
    }, 1000)
  } catch (error) {
    console.error('登录表单验证失败:', error)
  }
}
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100%;
  background-color: #f0f2f5;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding: 30px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
  
  .logo-icon {
    font-size: 40px;
    color: #1890ff;
    margin-bottom: 10px;
  }
  
  h2 {
    font-size: 20px;
    color: #1d2129;
    margin: 0;
  }
}

.login-form {
  .code-input {
    display: flex;
    gap: 10px;
    
    .code-img {
      width: 100px;
      height: 40px;
      cursor: pointer;
      
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: 4px;
      }
    }
  }
  
  .login-btn {
    width: 100%;
  }
}
</style>
